<template>
  <div class="register-page">
    <van-nav-bar
      title="面经注册"
    />
    <!-- 去vatn-ui里面找form组件 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button block type="warning" native-type="submit">提交</van-button>
      </div>
    </van-form>
    <!-- 注册的文字 -->
    <!-- <router-link to="/login"></router-link> -->
    <div class="lp-text van-cell"  @click="$router.push('/login')">有账号，去登录</div>
  </div>
</template>

<script>
// 按需导入，必须名称和导出名称写一致
import { registerRequest } from '@/api/user'

export default {
  name: 'RegisterPage',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      try {
        // async + await 这两个是promise的高级版，将异步的代码当做 同步的形式 来写
        await registerRequest({ username: this.username, password: this.password })

        this.$toast('请求成功')
        this.username = ''
        this.password = ''
        this.$router.push('/login')
      } catch (error) {
        // this.$toast('请求失败')
      }
    }
    // onSubmit (values) {
    //   registerRequest({ username: this.username, password: this.password }).then(res => {
    //     this.$toast('请求成功')
    //   }).catch(() => {
    //     this.$toast('请求成失败')
    //   })
    // }
  }
}
</script>

<style lang="less" scoped>
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
.van-nav-bar {
  border: 1px solid #ccc;
}
.lp-text {
  justify-content: end;
  top: -15px;
  color: lightblue;
}
</style>
